<template>
  <!-- 工时维护店端-经销商端页面 -->
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" labelWidth="85" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="repair-labourHour-index"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <!--列操作-->
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addEstimate">新增工时</el-button>
      </template>
    </FilterTable>

    <!--抽屉 -->
    <Drawer :visible.sync="estimate" :title="dialogTitle" :width="1100" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <labourHourInfo :type="handleType" :row="formData" @close="closeEstimate" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { getSeries } from '@/api/repair/estimateExtension';
import { getDealerLabourHourList, getAllSeries, getModelsBySeries } from '@/api/repair/repairBusiness/labourHour/index.js';
import labourHourInfo from '@/pages/repair/repairBusiness/labourHour/labourHourInfo';
import { getPinpSelect, getChexSelect, getCheModelSelect, getChexSelectTable, getCheModelSelectTable } from '@/api/repair/repairReception/customer/brand.js';

// 延保订单查询
export default {
  name: 'repair-labourHour-index',
  components: { labourHourInfo },
  mixins: [dictionary],
  data() {
    this.allSeries = {};
    return {
      fetchParams: { xhrAbort: false },
      dialogTitle: '',
      fetchApi: getDealerLabourHourList,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      selectFlag: false,
      topFilterList: this.createTopFilterList(),
      estimate: false,
      estimatequery: '' // 延保查询地址
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getAllSeries();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '工时代码',
          fieldName: 'labourCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '工时名称',
          fieldName: 'labourName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '工时状态',
          fieldName: 'itemStatus',
          placeholder: '请选择...',
          itemList: this.createDictList('1003')
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesCode',
          placeholder: '请选择...',
          itemList: [],
          change: val => {
            if (val) {
              let item = this.allSeries.find(x => x.seriesCode === val);
              if (item) {
                this.getModelsBySeries(item.seriesId);
              }
            }
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelLabourCode',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '数据来源',
          // initialValue: 10451001,
          fieldName: 'dataSources',
          placeholder: '请选择...',
          itemList: this.createDictList('1045')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          minWidth: '100',
          render: (props, h) => {
            return (
              <div>
                <el-button disabled={props.row.dataSources == 10451002} size="mini" type="text" onClick={() => this.emitFun(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.emitFun(props.row, 'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'itemStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: props => {
            return <span>{this.createDictText(props.row.itemStatus, '1003')}</span>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '工时代码',
          dataIndex: 'labourCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工时名称',
          dataIndex: 'labourName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '适用车型数量',
          dataIndex: 'num',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '下发时间',
          dataIndex: 'downAt',
          width: 150,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true
        },
        {
          title: '车型代码',
          dataIndex: 'modelCode',
          sorter: true,
          // editType: 'select',
          // editItems: [],
          filter: true,
          filterType: 'input'
        }
      ];
    },
    async getAllSeries() {
      let res = await getChexSelect('null');
      if (res.resultCode === 200) {
        let series = res.data.map(x => {
          return { value: x.seriesCode, text: x.seriesName };
        });
        this.topFilterList.find(x => x.fieldName === 'seriesCode').itemList = series;
        this.allSeries = res.data;
      }
    },
    async getModelsBySeries(val) {
      let res = await getCheModelSelect('null', val);
      if (res.resultCode === 200) {
        let models = res.data.map(x => {
          return { value: x.modelCode, text: x.modelName };
        });
        this.topFilterList.find(x => x.fieldName === 'modelLabourCode').itemList = models;
      }
    },

    emitFun(val, type) {
      this.handleType = type;
      this.dialogTitle = `工时信息`;
      this.formData = val;
      this.estimate = true;
    },

    addEstimate() {
      // 新建
      this.handleType = 'add';
      this.showHandler();
    },
    showHandler() {
      // 显示
      this.dialogTitle = `工时信息`;
      this.estimate = true;
    },
    selectData(val) {
      this.tableData = val;
      if (val.length == 0) {
        this.selectFlag = false;
      } else {
        this.selectFlag = true;
      }
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    closeEstimate(val, type) {
      // 关闭
      this.estimate = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
